/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiMain.java
 *
 * Created on Aug 22, 2011, 8:07:03 AM
 */
package Application;

import javax.swing.JOptionPane;
import javax.swing.GroupLayout;
import java.awt.Paint;

import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.KKLayout;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import org.apache.commons.collections15.Transformer;

import Graph.*;
import Redistribution.Redistribution;
import Parser.*;

import java.util.ArrayList;
import java.util.List;
import java.io.*;
import org.xml.sax.SAXException;

/**
 *
 * @author Olivier
 */
public class MainGUI extends javax.swing.JFrame {

	/** Creates new form GuiMain */
	public MainGUI() {
		step = 0;
		G1 = new Graph();
		G2 = new Graph();
		
		initComponents();
		initPanel();
	}
	
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				new MainGUI().setVisible(true);
			}
		});
	}
	
	private void initComponents() {
        GraphPanel1 = new javax.swing.JPanel();
		GraphPanel2 = new javax.swing.JPanel();
		GraphViewContainer = new javax.swing.JPanel();
		ButtonContainer = new javax.swing.JPanel();
        StepSwitcherButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        StepSwitcherButton.setText("Load XML Files");
        StepSwitcherButton.addActionListener(new java.awt.event.ActionListener() {
			@Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stepSwitcherClicked(evt);
            }
        });
    }
	
	private void initPanel () {
		GroupLayout layout = new GroupLayout(getContentPane());
		GroupLayout graphViewLayout = new GroupLayout(GraphViewContainer);
		GroupLayout buttonLayout = new GroupLayout(ButtonContainer);
		
		getContentPane().setLayout(layout);
		GraphViewContainer.setLayout(graphViewLayout);
		ButtonContainer.setLayout(buttonLayout);
		
		GraphPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.BLACK));
		GraphPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.BLACK));
		
		buttonLayout.setHorizontalGroup(
			buttonLayout.createSequentialGroup()
				.addGap(680)
				.addComponent(StepSwitcherButton, 200, 200, 200)
				.addGap(20)
		);
		
		buttonLayout.setVerticalGroup(
			buttonLayout.createSequentialGroup()
				.addGap(5)
				.addComponent(StepSwitcherButton, 23, 23, 23)
				.addGap(5)
		);
		
		graphViewLayout.setHorizontalGroup(
			graphViewLayout.createSequentialGroup()
				.addComponent(GraphPanel1, 447, 447, 447)
				.addGap(6)
				.addComponent(GraphPanel2, 447, 447, 447)
		);
		
		graphViewLayout.setVerticalGroup(
			graphViewLayout.createParallelGroup()
				.addComponent(GraphPanel1, 565, 565, 565)
				.addComponent(GraphPanel2, 565, 565, 565)
		);
		
		layout.setHorizontalGroup(
			layout.createSequentialGroup()
				.addGap(5)
				.addGroup(
					layout.createParallelGroup()
						.addComponent(GraphViewContainer, 905, 905, 905)
						.addComponent(ButtonContainer, 905, 905, 905)
				)
				.addGap(5)
		);
		
		layout.setVerticalGroup(
			layout.createSequentialGroup()
				.addGap(5)
				.addComponent(GraphViewContainer, 570, 570, 570)
				.addGap(5)
				.addComponent(ButtonContainer, 35, 35, 35)
				.addGap(5)
		);
		
        pack();
	}

	private void stepSwitcherClicked(java.awt.event.ActionEvent evt) {
		switch (step) {
			case 0:
				loadXMLFiles();
				drawGraphs(G1, G2);
				StepSwitcherButton.setText("Construct Compare Graphs");
				step++;
				
				break;
			case 1:
				redistribution = new Redistribution(G1, G2, G2.getColors(), 0.5, 1.0, 0.1, 0.5, 10);
				drawGraphs(redistribution.getFirstCompareGraph(), redistribution.getSecondCompareGraph());
				StepSwitcherButton.setText("Find MCS");
				step++;
				
				break;
			case 2:
				redistribution.doComparison();
				drawGraphs(redistribution.getNewGraph(), redistribution.getMCS());
				StepSwitcherButton.setText("Finish Coloration");
				step++;
				
				break;
			case 3:
				redistribution.doColoration();
				drawGraphs(new Graph(), redistribution.getNewGraph());
				StepSwitcherButton.setText("Quit");
				step++;
				
				break;
			case 4:
				System.exit(0);
				
				break;
			default: 
				JOptionPane.showMessageDialog(this, "An unknown action has been performed.", "Unknown Action", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void loadXMLFiles () {
		List<Variables> varsG1 = new ArrayList<Variables>();
		List<Variables> varsG2 = new ArrayList<Variables>();
		List<Color> colorsG1 = new ArrayList<Color>();
		List<Color> colorsG2 = new ArrayList<Color>();
		
		try {
			VariablesParser vParser = new VariablesParser();
			NodesParser nParser = new NodesParser();
			ColorsParser cParser = new ColorsParser();
			
			colorsG1 = cParser.getColorsFromFile("Datas/Colors/Colors-Thesis-Ex-G1.xml");
			G1.addColorListToGraph(colorsG1);
			varsG1 = vParser.getVariablesFromFile("Datas/Variables/Variables-Thesis-Ex-G1.xml");
			G1.addNodeListToGraph(nParser.getNodesFromFile("Datas/Nodes/Nodes-Thesis-Ex-G1.xml", varsG1, colorsG1));
			
			colorsG2 = cParser.getColorsFromFile("Datas/Colors/Colors-Thesis-Ex-G2.xml");
			G2.addColorListToGraph(colorsG2);
			varsG2 = vParser.getVariablesFromFile("Datas/Variables/Variables-Thesis-Ex-G2.xml");
			G2.addNodeListToGraph(nParser.getNodesFromFile("Datas/Nodes/Nodes-Thesis-Ex-G2.xml", varsG2, colorsG2));
			
			G1.generateEdges();
			G2.generateEdges();
		}
		catch (Exception ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void drawGraphs (Graph firstGraph, Graph secondGraph) {
		GraphPanel1.removeAll();
		GraphPanel2.removeAll();
		
		edu.uci.ics.jung.graph.Graph<Node, Edges> viewGraph1 = new edu.uci.ics.jung.graph.SparseGraph<Node, Edges>();
		edu.uci.ics.jung.graph.Graph<Node, Edges> viewGraph2 = new edu.uci.ics.jung.graph.SparseGraph<Node, Edges>();
		
		for (Node n : firstGraph.getNodes())
			viewGraph1.addVertex(n);
		
		for (Edges e : firstGraph.getEdges())
			viewGraph1.addEdge(e, e.getNode1(), e.getNode2());
		
		for (Node n : secondGraph.getNodes())
			viewGraph2.addVertex(n);
		
		for (Edges e : secondGraph.getEdges())
			viewGraph2.addEdge(e, e.getNode1(), e.getNode2());
		
		Layout<Node, Edges> layoutG1 = new KKLayout<Node, Edges>(viewGraph1);
		Layout<Node, Edges> layoutG2 = new KKLayout<Node, Edges>(viewGraph2);
		
		layoutG1.setSize(new java.awt.Dimension(440, 557));
		layoutG2.setSize(new java.awt.Dimension(440, 557));
		
		VisualizationViewer<Node, Edges> viewG1 = new VisualizationViewer<Node, Edges>(layoutG1);
		VisualizationViewer<Node, Edges> viewG2 = new VisualizationViewer<Node, Edges>(layoutG2);
		
		viewG1.setPreferredSize(new java.awt.Dimension(440, 557));
		viewG2.setPreferredSize(new java.awt.Dimension(440, 557));
		
		Transformer<Node, Paint> vertexPaint = new Transformer<Node, Paint>() {
			@Override
			public Paint transform(Node n) {
				if (n.getColor().getName().equals("Red"))
					return java.awt.Color.RED;
				else if (n.getColor().getName().equals("Green"))
					return java.awt.Color.GREEN;
				else if (n.getColor().getName().equals("Orange"))
					return java.awt.Color.ORANGE;
				else if (n.getColor().getName().equals("White"))
					return java.awt.Color.WHITE;
				else
					return java.awt.Color.WHITE;
			}
		};
		
		Transformer<Node, String> vertexToolTip = new Transformer<Node, String>() {
			@Override
			public String transform(Node n) {
				return n.getInstruction().getLabel();
			}
		};
		
		/*Transformer<Node, String> vertexLabel = new Transformer<Node, String>() {
			@Override
			public String transform(Node n) {
				return n.getInstruction().getLabel();
			}
		};*/
		
		viewG1.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
		//viewG1.getRenderContext().setVertexLabelTransformer(vertexLabel);
		viewG1.setVertexToolTipTransformer(vertexToolTip);
		viewG2.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
		//viewG2.getRenderContext().setVertexLabelTransformer(vertexLabel);
		viewG2.setVertexToolTipTransformer(vertexToolTip);
		
		GraphPanel1.add(viewG1);
		GraphPanel2.add(viewG2);
		
		GraphPanel1.revalidate();
		GraphPanel2.revalidate();
		repaint();
	}
	
    private javax.swing.JPanel GraphPanel1;
	private javax.swing.JPanel GraphPanel2;
	private javax.swing.JPanel GraphViewContainer;
	private javax.swing.JPanel ButtonContainer;
    private javax.swing.JButton StepSwitcherButton;
	
	private Graph G1;
	private Graph G2;
	private Redistribution redistribution;
	
	private int step;
}

